package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imut.lagain.entity.CareMessage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 关怀消息数据访问层
 */
@Mapper
public interface CareMessageRepository extends BaseMapper<CareMessage> {

    /**
     * 根据接收者ID查询消息
     * @param toUserId 接收者ID
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE to_user_id = #{toUserId} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByToUserId(@Param("toUserId") Long toUserId);

    /**
     * 根据发送者ID查询消息
     * @param fromUserId 发送者ID
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE from_user_id = #{fromUserId} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByFromUserId(@Param("fromUserId") Long fromUserId);

    /**
     * 查询两个用户之间的消息
     * @param userId1 用户1 ID
     * @param userId2 用户2 ID
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE ((from_user_id = #{userId1} AND to_user_id = #{userId2}) OR (from_user_id = #{userId2} AND to_user_id = #{userId1})) AND is_deleted = 0 ORDER BY created_at ASC")
    List<CareMessage> findBetweenUsers(@Param("userId1") Long userId1, @Param("userId2") Long userId2);

    /**
     * 根据用户ID和关怀类型查询消息
     * @param userId 用户ID（发送者或接收者）
     * @param careType 关怀类型
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE (from_user_id = #{userId} OR to_user_id = #{userId}) AND care_type = #{careType} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByUserIdAndCareType(@Param("userId") Long userId, @Param("careType") String careType);

    /**
     * 查询用户在指定时间范围内的消息
     * @param userId 用户ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE (from_user_id = #{userId} OR to_user_id = #{userId}) AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByUserIdAndDateRange(
            @Param("userId") Long userId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);

    /**
     * 查询未读消息
     * @param toUserId 接收者ID
     * @return 未读消息列表
     */
    @Select("SELECT * FROM care_message WHERE to_user_id = #{toUserId} AND status != 'read' AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findUnreadMessages(@Param("toUserId") Long toUserId);

    /**
     * 标记消息为已读
     * @param messageId 消息ID
     * @param readAt 阅读时间
     * @return 更新行数
     */
    @Update("UPDATE care_message SET status = 'read', read_at = #{readAt}, updated_at = NOW() WHERE id = #{messageId} AND is_deleted = 0")
    int markAsRead(@Param("messageId") Long messageId, @Param("readAt") LocalDateTime readAt);

    /**
     * 统计用户发送的消息数量
     * @param fromUserId 发送者ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 消息数量
     */
    @Select("SELECT COUNT(*) FROM care_message WHERE from_user_id = #{fromUserId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0")
    int countSentMessages(
            @Param("fromUserId") Long fromUserId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);

    /**
     * 统计用户接收的消息数量
     * @param toUserId 接收者ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 消息数量
     */
    @Select("SELECT COUNT(*) FROM care_message WHERE to_user_id = #{toUserId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0")
    int countReceivedMessages(
            @Param("toUserId") Long toUserId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);

    /**
     * 根据生理期阶段查询消息
     * @param userId 用户ID
     * @param cyclePhase 生理期阶段
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE (from_user_id = #{userId} OR to_user_id = #{userId}) AND cycle_phase = #{cyclePhase} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByUserIdAndCyclePhase(@Param("userId") Long userId, @Param("cyclePhase") String cyclePhase);

    /**
     * 查询自动消息
     * @param userId 用户ID
     * @param isAuto 是否自动消息
     * @return 消息列表
     */
    @Select("SELECT * FROM care_message WHERE (from_user_id = #{userId} OR to_user_id = #{userId}) AND is_auto = #{isAuto} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareMessage> findByUserIdAndIsAuto(@Param("userId") Long userId, @Param("isAuto") Boolean isAuto);
}
